package com.cwvs.controller.admin;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.MapUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.cwvs.helper.UrlHelper;
import com.cwvs.services.AppAdvertService;
import com.cwvs.utils.CommonUtils;
import com.cwvs.utils.Pages;

/**
 * 广告Controller
 * 
 * @author Administrator
 * 
 */
@Controller
@RequestMapping("/admin")
public class AdminAppAdvertController {
	@Resource
	private AppAdvertService appAdvertService;
	@Resource
	private UrlHelper urlHelper;
	@RequestMapping("/advert_view")
	public ModelAndView advertView(HttpServletRequest request,
			HttpServletResponse response) {
		ModelAndView mv = new ModelAndView("app_advert/app_advert_list");
		return mv;
	}
	@RequestMapping("/advert_list")
	public @ResponseBody
	Map<String, Object> storeList(HttpServletRequest request,
			HttpServletResponse response) {
		Pages page = new Pages();
		String limit = request.getParameter("limit");
		String start = request.getParameter("start");
		page.setPageSize(Integer.parseInt(limit));
		page.setCurrentPage(Integer.parseInt(start) / Integer.parseInt(limit)
				+ 1);
		HashMap<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("NAME", request.getParameter("name"));
		Pages rePage = appAdvertService.advertList(paramsMap, page);
		Map<String, Object> ret = new HashMap<String, Object>();
		ret.put("rows", rePage.getPage());
		ret.put("totalCount", rePage.getTotalRecord());
		return ret;
	}
	/**
	 * 广告保存
	 * @param request
	 * @param response
	 * @param file
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	@RequestMapping("/advert_save")
	public @ResponseBody
	Map<String, Object> advertSave(HttpServletRequest request,
			HttpServletResponse response, @RequestParam("photo-path") MultipartFile file) {
		Map<String,Object> map=new HashMap<String,Object>();
		Enumeration paramNames = request.getParameterNames();
		while (paramNames.hasMoreElements()) {
			String paramName = (String) paramNames.nextElement();
			String[] paramValues = request.getParameterValues(paramName);
			if (paramValues.length == 1) {
				String paramValue = paramValues[0];
				if (paramValue.length() != 0) {
					map.put(paramName.toUpperCase(), paramValue);
				}
			}
		}
		List<String> fileTypes=new ArrayList<>();
		fileTypes.add("jpg");
		fileTypes.add("gif");
		fileTypes.add("png");
		fileTypes.add("jpeg");  
        fileTypes.add("bmp");  
        map.put("PIC_URL", CommonUtils.getFile(urlHelper.getUploadFile(),file, "advert", fileTypes));
        map.put("STIME",  CommonUtils.stringToDateTime(MapUtils.getString(map, "STIME")));
        map.put("ETIME",  CommonUtils.stringToDateTime(MapUtils.getString(map, "ETIME")));
		Map<String,Object> res=new HashMap<String,Object>();
		boolean b=appAdvertService.saveOrUpdate(map);
		if(b){
			res.put("message", "保存成功");
		}else{
			res.put("errormsg", "保存失败");
		}
		res.put("success", b);
		return res;
	}
	@RequestMapping("/advert_edit")
	public ModelAndView storeEdit(HttpServletRequest request,
			HttpServletResponse response) {
		ModelAndView mv = new ModelAndView("app_advert/app_advert_edit");
		return mv;
	}
	/**
	 * 广告停用
	 * @param request
	 * @param response
	 * @param file
	 * @return
	 */
	@RequestMapping("/advert_false")
	public @ResponseBody
	Map<String, Object> advertFalse(HttpServletRequest request,
			HttpServletResponse response){
		Map<String,Object> res=new HashMap<String,Object>();
		String id=request.getParameter("id");
		boolean b=appAdvertService.advertSetStaus(id, false);
		if(b){
			res.put("message", "处理成功");
		}else{
			res.put("errormsg", "处理失败");
		}
		res.put("success", b);
		return res;
	}
	
	/**
	 * 广告启用
	 * @param request
	 * @param response
	 * @param file
	 * @return
	 */
	@RequestMapping("/advert_true")
	public @ResponseBody
	Map<String, Object> advertTrue(HttpServletRequest request,
			HttpServletResponse response){
		Map<String,Object> res=new HashMap<String,Object>();
		String id=request.getParameter("id");
		boolean b=appAdvertService.advertSetStaus(id, true);
		if(b){
			res.put("message", "处理成功");
		}else{
			res.put("errormsg", "处理失败");
		}
		res.put("success", b);
		return res;
	}
	
	/**
	 * 更新序号
	 */
	@RequestMapping("/advert_sort")
	public @ResponseBody
	Map<String, Object> advertSortNum(HttpServletRequest request,
			HttpServletResponse response){
		Map<String,Object> res=new HashMap<String,Object>();
		String id=request.getParameter("id");
		String sortNum=request.getParameter("sortNum");
		int sort=1;
		try {
			sort=Integer.parseInt(sortNum);
		} catch (NumberFormatException e) {
		}
		boolean b=appAdvertService.advertSortNum(id, sort);
		if(b){
			res.put("message", "处理成功");
		}else{
			res.put("errormsg", "处理失败");
		}
		res.put("success", b);
		return res;
	}
}
